<% provide(:title, document.title) %>

<% add_decidim_meta_tags(
  title: document.title,
  url: document_url(document.id),
  resource: document) %>

<%
  edit_link(
    resource_locator(document).edit,
    :update,
    :collaborative_text,
    document:
  )
%>

<% content_for :aside do %>
  <div class="collaborative-texts-toc" data-collaborative-texts-toc="collaborative-text-<%= document.id %>">
    <h4 class="h4 mb-4"><%= t("decidim.collaborative_texts.document.index") %></h4>
    <ul class="spinner-container"></ul>
  </div>
<% end %>

<%= render layout: "layouts/decidim/shared/layout_two_col", locals: { columns: 10, large: true } do %>
  <%= render partial: "decidim/shared/component_announcement" %>

  <section class="layout-main__section">
    <div class="flex justify-center">
      <h1 class="title-decorator my-12"><%= document.title %></h1>
    </div>

    <%= cell("decidim/announcement", translated_attribute(document.announcement)) if strip_tags(document.announcement&.values&.join)&.strip.present? %>
    <%= cell("decidim/announcement", t("decidim.collaborative_texts.document.status.draft"), callout_class: "warning") if document.draft? %>
    <%= cell("decidim/announcement", t("decidim.collaborative_texts.document.status.accepting_suggestions")) if document.suggestions_enabled? %>
    <%= cell("decidim/announcement", t("decidim.collaborative_texts.document.status.not_accepting_suggestions")) unless document.suggestions_enabled? || document.draft? %>

    <div class="collaborative-texts-manager hidden"><%= render "manager" %></div>

    <div class="collaborative-texts-alert hidden">
      <div></div>
    </div>

    <script type="text/template" class="decidim-template" id="collaborative-texts-editor-template"><%= render "editor_template" %></script>
    <script type="text/template" class="decidim-template" id="collaborative-texts-suggestions-box-template"><%= render "suggestions_box_template" %></script>
    <script type="text/template" class="decidim-template" id="collaborative-texts-suggestions-box-item-template"><%= render "suggestions_box_item_template" %></script>

    <div id="collaborative-text-<%= document.id %>" data-collaborative-texts-document="<%= document.suggestions_enabled? %>"
         data-collaborative-texts-i18n="<%= document_i18n.to_json %>"
         data-collaborative-texts-suggestions-editor-template="#collaborative-texts-editor-template"
         data-collaborative-texts-suggestions-box-template="#collaborative-texts-suggestions-box-template"
         data-collaborative-texts-suggestions-box-item-template="#collaborative-texts-suggestions-box-item-template"
         data-collaborative-texts-suggestions-url="<%= document_suggestions_path(document) %>"
         data-collaborative-texts-rollout-url="<%= document_path(document) if allowed_to?(:rollout, :collaborative_text, document:) %>"
         class="collaborative-texts-container editor-content">
      <%= decidim_sanitize decidim_rich_text(document.consolidated_body), { scrubber: Decidim::AdminInputScrubber.new } %>
    </div>

    <hr class="text-gray-6 my-12">

    <% if document.coauthorships.any? %>
      <div class="my-12">
        <h5 class="h5 my-4"><%= t("decidim.collaborative_texts.documents.show.contributors") %></h5>
        <%= cell "decidim/coauthorships", document, context_actions: [:date] %>
      </div>
    <% end %>
  </section>
<% end %>

<% append_javascript_pack_tag("decidim_collaborative_texts") %>
<% append_stylesheet_pack_tag("decidim_collaborative_texts") %>
